Sandro Tosi: Re: Converting date to epoch
Alexander,did you even consider that I might need to convert a date to epoch in a python script? :)
>>> import time
>>> str = '2009-03-04'
>>> format = '%Y-%m-%d'
>>> time.mktime(time.strptime(str, format))
1236121200.0
>>> int(_)
1236121200
strace $(pgrep httpd awk ' print "-p " $1 ' tr '\n' ' ')
$ curl -H "Host: domain" http://ip_address/path/to/the/page.html
import httplib
conn = httplib.HTTPConnection("ip_address")
conn.putrequest("GET", "/", skip_host=True)
conn.putheader("Host", "domain.ext")
conn.endheaders()
res = conn.getresponse()
print res.read()
udd=> select source, all_bugs from bugs_count where source = 'reportbug';but the result is the same: less than 100 bugs against reportbug! Yay!
source all_bugs
----------+----------
reportbug 91
def collatz(n):very rapidly converges to... an error:
if n == 1:
return 1
if n % 2 == 0:
return 1 + collatz(n/2)
else:
return 1 + collatz(3*n + 1)
RuntimeError: maximum recursion depth exceededSo I've recoded it a bit using a cache dictionary to store all the intermediate values in it:
cache = 1: 1Now loping for all the numbers lower than 1 million we find the solution to the problem: the number with the longest path is 837799 with a path length of 524. The code runs in about 4secs.
def collatz(n, res):
nn = n
while True:
if n in cache:
if nn != n:
cache[nn] = cache[n]
return cache[nn]
if n % 2 == 0:
cache[n] = 1 + collatz(n/2, cache)
else:
cache[n] = 1 + collatz(3*n + 1, cache)
<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css'
rel='stylesheet' type='text/css'/>
<style type='text/css'>
.pln color: #C0C0C0;
.kwd color: #4169E1;
.tag color: #4169E1;
</style>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js'
type='text/javascript'> </script>
Next.